#ifndef SUBSCRIBER_H
#define SUBSCRIBER_H
#include <chrono>
#include <list>
#include <string>
#include <vector>
#include <atomic>
#include "ThostFtdcMdApi.h"

typedef struct
{
	char exchange_id[20];
	char exchange_name[100];
	char product_id[30];
	char product_name[100];
	double price;
	int quantity;
	int trade_volume;
	double high;
	double low;
	double settle;
	double prev_close;
	int openint;
	int prev_openint;
	double average_price;
	double buy_price;
	int buy_quantity;
	double sell_price;
	int sell_quantity;
	double open_price;
	double prev_settle;
	double min_movement;
	double high_limit;
	double low_limit;
	int precision;
	bool subscribed;
	char expired_date[20];
	int product_type;
	char product[20];
	double margin_ratio;
	int multiple;
	char tradingday[11];
} quotation;

class MarketDataSubscriber : public CThostFtdcMdSpi
{
public:
	std::atomic<bool> ready;
	CThostFtdcMdApi *mdApi;
	MarketDataSubscriber(std::string brokerID, std::string userID, std::string password,std::string quoteAddress, char* instruments[], int size);
	void OnFrontConnected();
	void OnFrontDisconnected(int Reason);
	void OnRspUserLogin(CThostFtdcRspUserLoginField *pRspUserLogin, CThostFtdcRspInfoField *pRspInfo, int requestID, bool bIsLast);
	void SubscribeMDList(char* instrumentList[], int size);
	void OnRtnDepthMarketData(CThostFtdcDepthMarketDataField *pDepthMarketData);
	void OnRspSubMarketData(CThostFtdcSpecificInstrumentField *pSpecificInstrument, CThostFtdcRspInfoField *rspInfo, int nRequestID, bool bIsLast);
	void HandleIndepthMarketData(CThostFtdcDepthMarketDataField &depthMarketData);

private:
	std::chrono::steady_clock::time_point reqtime;
	std::chrono::steady_clock::time_point rsptime;
	std::string brokerID;
	std::string userID;
	std::string password;
	char** instruments;
	int size;
};

#endif